<script setup>
import { onMounted, onUnmounted, ref } from "vue";
import router from "@/router/index";
import { _$, $ } from "./edit";

const uploadServerUrl = `${import.meta.env.VITE_API_SERVER}`;
const formRef = ref();

onMounted(() => {
  $.init([formRef]);
  if (router.currentRoute.value.query.id) {
    _$.requestParam.id = router.currentRoute.value.query?.id;
    $.loadEdit();
  }
});

onUnmounted(() => {
  $.clean();
});
</script>

<template>
  <el-space direction="vertical" fill wrap class="w-fill" :size="20">
    <el-page-header @back="router.back()" size="small">
      <template #content>
        <span class="text-large font-600 mr-3">食谱信息编辑</span>
      </template>
    </el-page-header>
    <el-card class="w-fill" shadow="never">
      <el-space wrap class="w-fill" :size="20">
        <el-button type="info" size="small" @click="$.onReset(formRef)"
          >重置</el-button
        >
        <el-button type="primary" size="small" @click="$.onSubmit(formRef)"
          >提交</el-button
        >
      </el-space>
    </el-card>
    <el-card class="w-fill" shadow="never">
      <el-descriptions title="基本信息">
        <el-descriptions-item>
          <el-form
            ref="formRef"
            :model="_$.requestParam"
            :rules="_$.rules"
            label-width="auto"
            size="small"
            style="max-width: 600px"
          >
            <el-form-item label="食谱名称" prop="name">
              <el-input v-model="_$.requestParam.name" />
            </el-form-item>
            <el-form-item label="食谱内容" prop="content">
              <el-input v-model="_$.requestParam.content" />
            </el-form-item>
            <el-form-item label="食谱描述" prop="text">
              <el-input v-model="_$.requestParam.text" />
            </el-form-item>
            <el-form-item label="食谱类型" prop="typeId">
              <el-input v-model="_$.requestParam.typeId" />
            </el-form-item>
            <el-form-item label="食谱图片信息" prop="images">
              <el-upload
                v-model:file-list="_$.fileList"
                action="/ats/recipe/upload"
                :headers="_$.headers"
                list-type="picture-card"
                :on-preview="$.onPreview"
                :on-success="$.onUploadSuccess"
                :on-remove="$.onRemove"
              >
                <el-icon><Plus /></el-icon>
              </el-upload>
              <el-dialog v-model="_$.state.previewDialogVisible">
                <img
                  w-full
                  :src="_$.state.dialogImageUrl"
                  alt="Preview Image"
                />
              </el-dialog>
            </el-form-item>
            <el-form-item label="食谱会导致的过敏集合" prop="recipeAllergyList">
              <el-select v-model="_$.requestParam.recipeAllergyList" multiple>
                <el-option
                  v-for="item in _$.allergies"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="食谱适宜人群集合" prop="recipeCureDiseaseList">
              <el-select
                v-model="_$.requestParam.recipeCureDiseaseList"
                multiple
              >
                <el-option
                  v-for="item in _$.diseases"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="食谱不适宜人群集合" prop="recipeDiseaseList">
              <el-select v-model="_$.requestParam.recipeDiseaseList" multiple>
                <el-option
                  v-for="item in _$.diseases"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </el-descriptions-item>
      </el-descriptions>
    </el-card>
  </el-space>
</template>

<style scoped>
.upload-control {
  width: 100%;
}
</style>
